/*
 * @copyright: Huang Ding
 * @Author: ding-cx
 * @Date: 2021-02-26 16:57:25
 * @LastEditors: ding-cx
 * @LastEditTime: 2021-04-02 10:03:39
 * @Description: file content
 */
import { Application } from "egg";
import { DataTypes, Model } from "sequelize";
import { modelCommonField } from "../../utils";

export interface sysMenusInterface extends Model {
  /** 主键id */
  id: number;
  /** 父菜单id */
  parent_menu_id: number;
  /** 菜单排序号 */
  order: number;
  /** 菜单的route name，同时也是path */
  path: string;
  /** 菜单名称，侧边栏显示名称 */
  title: string;
  /** 组件名 基于前端views目录*/
  component: string;
  /** 重定向路径 */
  redirect: string;
  /** 是否隐藏，不在侧边栏显示 */
  is_hidden: boolean;
  /** 是否总是在侧栏显示根路由 */
  always_show: boolean;
  /** 菜单图标 */
  icon: string;
  /** 不缓存 */
  no_cache: boolean;
  /** 固定在tagview */
  affix: boolean;
  /** 是否展示在面包屑 */
  breadcrumb: boolean;
  /** 当前页面激活左侧菜单的路径 */
  active_menu: boolean;
  /** 路由组件name选项 */
  component_name: string;
}

export default (app: Application) => {
  const model = app.model.define<sysMenusInterface>(
    app.config.dbModel_prefix + "sys_menus",
    {
      parent_menu_id: {
        field: "parent_menu_id",
        comment: "父菜单id", //没有父级则为0
        type: DataTypes.INTEGER,
      },
      order: {
        field: "order",
        comment: "菜单排序号", //没有父级则为0
        type: DataTypes.INTEGER,
      },
      path: {
        field: "path",
        comment: "菜单的routename,同时也是path",
        type: DataTypes.STRING(60),
      },
      title: {
        field: "title",
        comment: "侧边栏显示名称",
        type: DataTypes.STRING(30),
      },
      component: {
        field: "component",
        comment: "组件名，基于views",
        type: DataTypes.STRING(200),
      },
      redirect: {
        field: "redirect",
        comment: "重定向路径",
        type: DataTypes.STRING(200),
      },
      is_hidden: {
        field: "is_hidden",
        comment: "是否隐藏，不在侧边栏的显示",
        type: DataTypes.BOOLEAN,
      },
      always_show: {
        field: "always_show",
        comment: "是否总是在侧栏显示根路由",
        type: DataTypes.BOOLEAN,
      },
      icon: {
        field: "icon",
        comment: "菜单图标",
        type: DataTypes.STRING(50),
      },
      no_cache: {
        field: "no_cache",
        comment: "不缓存",
        type: DataTypes.BOOLEAN,
      },
      affix: {
        field: "affix",
        comment: "固定在tagview",
        type: DataTypes.BOOLEAN,
      },
      breadcrumb: {
        field: "breadcrumb",
        comment: "是否展示在面包屑",
        type: DataTypes.BOOLEAN,
      },
      active_menu: {
        field: "active_menu",
        comment: "当前页面激活左侧菜单的路径",
        type: DataTypes.STRING(200),
      },
      component_name: {
        field: "component_name",
        comment: "对应路由组件的name选项",
        type: DataTypes.STRING(120),
      },
      ...modelCommonField,
    }
  );

  /* 创建关联关系 hasOne是我的id对应as的parent_menu_id*/
  // model.hasOne(model, {
  //   foreignKey: "parent_menu_id",
  //   as: "parentMenu",
  // });
  (model as any).associate = () => {
    model.belongsTo(model, {
      foreignKey: "parent_menu_id",
      as: "parent_menu",
      constraints: false,
    });
  };

  // 固定默认插入数据库的基本菜单数据
  const baseMenus = [
    {
      id: 1,
      parent_menu_id: 0,
      title: "系统管理",
      path: "sys",
      component: "/",
      icon: "dashboard",
    },
    {
      id: 2,
      parent_menu_id: 1,
      title: "菜单管理",
      path: "menus",
      component: "sys/menus",
      icon: "nested",
    },
    {
      id: 3,
      parent_menu_id: 1,
      title: "用户管理",
      path: "user",
      component: "sys/user",
      icon: "user",
    },
    {
      id: 4,
      parent_menu_id: 1,
      title: "权限管理",
      path: "permission",
      component: "layout",
      icon: "user",
    },
    {
      id: 5,
      parent_menu_id: 4,
      title: "角色管理",
      path: "role",
      component: "sys/role",
      icon: "el-icon-user",
    },
    {
      id: 6,
      parent_menu_id: 4,
      title: "自定义权限",
      path: "custom-perm",
      component: "sys/permission",
      icon: "el-icon-user",
    },
    {
      id: 7,
      parent_menu_id: 1,
      title: "系统日志",
      path: "log",
      component: "sys/log",
      icon: "el-icon-document",
    },
    {
      id: 8,
      parent_menu_id: 1,
      title: "字典管理",
      path: "dic",
      component: "sys/dic",
      icon: "el-icon-document",
    },
    {
      id: 9,
      parent_menu_id: 1,
      title: "组织机构",
      path: "org",
      component: "sys/org",
      icon: "el-icon-s-opportunity",
    },
    {
      id: 10,
      parent_menu_id: 1,
      title: "系统配置",
      path: "config",
      component: "sys/config",
      icon: "el-icon-setting",
    },
    {
      id: 11,
      parent_menu_id: 1,
      title: "文件上传",
      path: "oss",
      component: "sys/oss",
      icon: "el-icon-document",
    },
  ];
  baseMenus.forEach((item) => {
    model.findOrCreate({
      defaults: item,
      where: {
        parent_menu_id: item.parent_menu_id,
        path: item.path,
      },
    });
  });

  return model;
};
